package com.apes.sms.masterdata.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.audit.Auditing;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Brand;
import com.apes.scm.masterdata.product.model.Category;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * Created by lishikun on 2021/12/20 9:30
 */
@Auditing
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SupplierProduct.CONST_TABLE_NAME, indexes = {
        @Index(columnList = "supplier_id, uuid")})
public class SupplierProduct extends Model implements Cloneable {
    public static final String CONST_TABLE_NAME = "sms_supplier_product";

    @Id
    @Column(name = "id", length = 10)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "供应商商品表", padding = 10)
    @Comment("商品编码")
    private String id; //商品编码

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
    @Comment("供应商编码")
    private PartyRole supplier;

    @Column(name = "supplier_platform_item_id", length = 120)
    @Comment("平台供应商编码")
    private String supplierPlatformItemId;

    @Column(name = "supplier_platform_source", length = 120)
    @Comment("供应商所在平台内部码")
    private String supplierPlatformSource;

    @States(name = "状态", values = {
            @State(id = "toBeImproved", name = "待完善"),
            @State(id = "toBeAudit", name = "待审核"),
            @State(id = "toBeShelf", name = "待上架"),
            @State(id = "onShelf", name = "上架"),
            @State(id="delete",name="删除")})
    @Column(name = "state", nullable = false, length = 30)
    @Comment("状态")
    private String state;

    @NotNull
    @Column(name = "uuid", nullable = false, length = 100)
    @Comment("唯一编码(供应商对该商品的唯一编码)---商家字段")
    private String uuid;

    @NotNull
    @Column(name = "business_id", nullable = false, length = 100)
    @Comment("商家编码(供应商对该商品的编码)---商家字段")
    private String businessId;

    @Comment("品牌名称---商家字段")
    @Column(name = "brand_name", length = 100)
    private String brandName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sub_quality", referencedColumnName = "id")
    @Comment("细分品质(老系统使用)---商家字段")
    private Enumeration subQuality; //细分品质

    @Comment("零件名称---商家字段")
    @Column(name = "part_name", length = 100)
    private String partName;

    @Comment("通用件号---商家字段")
    @Column(name = "universal_part_number", length = 100)
    private String universalPartNumber;

    @Comment("品牌件型号---商家字段")
    @Column(name = "brand_model", length = 100)
    private String brandModel;

    @Comment("产品描述---商家字段")
    @Column(name = "product_description", length = 500)
    private String productDescription;

    @Comment("计量单位---商家字段")
    @Column(name = "uom_name", length = 16)
    private String uomName;

    @Comment("产地---商家字段")
    @Column(name = "origin", length = 100)
    private String origin;

    @Comment("尺寸---商家字段")
    @Column(name = "sizes", length = 100)
    private String sizes;

    @Comment("包装含量---商家字段")
    @Column(name = "package_content", length = 200)
    private String packageContent;


    @Comment("不含税单价---库存价格")
    @Column(name = "untax_price", columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double untaxPrice;

    @Comment("库存数量---库存价格")
    @Column(name = "inventory_quantity", columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double inventoryQuantity;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "brand_id", referencedColumnName = "id")
    @Comment("对照品牌---对照字段")
    private Brand contrastBrand;

    @Comment("原厂编码---对照字段")
    @Column(name = "original_factory_code", length = 100)
    private String originalFactoryCode;

    @Column(name = "contrast_origin", length = 100)
    @Comment("对照产地---对照字段")
    private String contrastOrigin;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id", referencedColumnName = "id")
    @Comment("对照配件名称---对照字段")
    private Category category;

    @Comment("对照包装含量---对照字段")
    @Column(name = "contrast_package_content", length = 200)
    private String contrastPackageContent;

    @Column(name = "remark", length = 200)
    @Comment("备注")
    private String remark;

    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
        }
        return null;
    }
}
